package com.gzwanchuang.lszg.module.shop;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gzwanchuang.lszg.R;
import com.gzwanchuang.lszg.adapter.ShopAdapter;
import com.gzwanchuang.lszg.base.BaseActivity;
import com.gzwanchuang.lszg.net.Apis;
import com.gzwanchuang.lszg.net.NetworkManager;
import com.gzwanchuang.lszg.net.interfaces.OnRequestCallBack;
import com.gzwanchuang.lszg.proto.Mystock;
import com.gzwanchuang.lszg.utils.StatusBarUtil;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ShopActivity extends BaseActivity {

    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_message)
    TextView tvMessage;


    private ShopAdapter mAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
        ButterKnife.bind(this);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#ffffff"), true);
        tvMessage.setMovementMethod(new ScrollingMovementMethod());
        tvTitle.setText("我的商户");
        initRecyclerView();
        initListener();
        loadData();
    }

    private void initListener() {
        backIv.setOnClickListener(view -> finish());
    }

    private void loadData() {
        NetworkManager.INSTANCE.post(Apis.goods_merchant, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                Mystock.goods_merchnat result = Mystock.goods_merchnat.parseFrom(response);
                Log.e("wang", "onOk: " + result.toString());
                List<Mystock.list_basis> list = result.getListDesList();
                List<Mystock.goods> list_goods = result.getListList();
                StringBuilder des = new StringBuilder();
                for (int i = 0; i < list.size() - 1; i++) {
                    des.append(list.get(i).getName()).append("\n");
                }
                tvMessage.setText(des.toString());
                mAdapter.addData(list_goods);
                mAdapter.setUseEmpty(true);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                mAdapter.setUseEmpty(true);
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();

    }

    private void initRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter = new ShopAdapter(mContext));
        mAdapter.setEmptyView(R.layout.recycle_empty);
        mAdapter.setUseEmpty(false);
        // 每个item的点击事件
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(mContext,ShopNextActivity.class);
            intent.putExtra("title",mAdapter.getData().get(position).getGoodsName()+" "+mAdapter.getData().get(position).getGoodsSerial())  ;
            intent.setType(mAdapter.getData().get(position).getGoodsCommonid());
            startActivity(intent);
        });
    }

}
